//
//  QSWindowFilter.m
//
//  Created by Nicholas Jitkoff on 11/20/05.
//  Copyright 2005 Blacktree, All rights reserved.
//

#import "QSWindowFilter.h"

@implementation QSWindowFilter

- (id)initWithWindow:(NSWindow *)window {
  if ((self = [super init])) {
    SInt32 systemVersion = 0;
    OSStatus err = Gestalt(gestaltSystemVersion, &systemVersion);
    if (err != noErr || systemVersion < 0x1050) {
      [self release];
      return nil;
    }
    wid = [window windowNumber];
  }
  return self;
}

- (void)setFilterName:(NSString *)filterName {
	if (fid) {
		CGSRemoveWindowFilter(_CGSDefaultConnection(), wid, fid);
		CGSReleaseCIFilter(_CGSDefaultConnection(), fid);
	}
	if (filterName) {
		CGError error = CGSNewCIFilterByName(_CGSDefaultConnection(), (CFStringRef) filterName, &fid);
		if ( noErr == error ) {
			error = CGSAddWindowFilter(_CGSDefaultConnection(), wid, fid, 0x00003001);
		}
	}
}

- (void)setFilterValues:(NSDictionary *)filterValues {
  if (!fid) return;
  CGSSetCIFilterValuesFromDictionary(_CGSDefaultConnection(), fid, (CFDictionaryRef) filterValues);
}

- (void)dealloc {
  [self setFilterName:nil];
  [super dealloc];
}

@end
